<?php
//current page 当前页面
//total number of records 总记录数
//pagesize页面大小
//Number of links 链接数目
//request parameter
//intarray(当前页，数组的长度即总记录
//数，)

//$array要分页显示的数组
//$nCurrentPage当前页
//&$content当前显示数的内容
//$url 当前页面的url
//$requestParas='&' 例如 &str=dfadf&action=del
//$pageSize 每页显示的记录数，每页显示数组的多少项
//显示的链接数量  首页 第一页 1 2 3 4 5 6 7 8 9 10 11 末页
//此例中$numberOfLinks=11
function _array_pagination($array,$nCurrentPage=2,&$content,$url='',$requestParas='&',$pageSize=50,$numberOfLinks=8,$totalNumberOfRecords='empty'){
	$tNORes       ; $nCurrentPageMax  ; $nCurrentPage ;
    $nCurrentPage ; $nL               ;  $arrKeyStart ;
	CaPagination::checkVars($array,$nCurrentPage,$content,$url,$requestParas,$pageSize,$numberOfLinks ,$totalNumberOfRecords,$tNORes,$nCurrentPageMax,$arrKeyStart,$nL);
    //向数组填充内容
	CaPagination::fillContent($array,$arrKeyStart,$pageSize,&$content);
	
    //计算输出的链接数
    $countOutLinksNum =   0   ;
	//计算第一个链接的page值
	$startLinkVal    = 'empty';
	//计算最后一个链接的page值
	$endLinkVal      = 'empty';
    
	$outlinks = CaPagination::outlinks($url,$requestParas,$numberOfLinks,$nL,$nCurrentPage,$nCurrentPageMax
	,$countOutLinksNum,$endLinkVal,$startLinkVal);
 
	$ret=CaPagination::firstAndPrevious($url,$requestParas,$nCurrentPage)
	.CaPagination::insertLinksAtBeg(
	$url,$requestParas,$numberOfLinks,$countOutLinksNum,$startLinkVal)
	.$outlinks
	.CaPagination::insertLinksAtEnd(
	$url,$requestParas,$numberOfLinks,$countOutLinksNum,$nCurrentPageMax,$endLinkVal
	)
	.CaPagination::nextAndUltimate($url,$requestParas,$nCurrentPage,$nCurrentPageMax)
	.CaPagination::autogo($url,$requestParas);

	return  '<div class="pagehref" style="border:1px solid #888; margin-top:-20px;">'.$ret.'</div>';
}

class CaPagination{
	public static function checkVars($array,&$nCurrentPage,&$content,$url,$requestParas,$pageSize,$numberOfLinks ,$totalNumberOfRecords,&$tNORes,&$nCurrentPageMax,&$arrKeyStart,&$nL){
	$tNORes=$totalNumberOfRecords=='empty'? count($array) : $totalNumberOfRecords;
    
	$nCurrentPageMax=ceil( $tNORes / $pageSize );
	
	$nCurrentPage = preg_replace("/[^0-9]{1,}/","",$nCurrentPage);
	 
	$nCurrentPage = ( empty($nCurrentPage) )? 6 : $nCurrentPage ;
	if($nCurrentPage>$nCurrentPageMax){$nCurrentPage=$nCurrentPageMax;}
	
	//$nL 为nLinkValutStart; 
	$nL=($nCurrentPage-floor($numberOfLinks/2)-1);
	
	$arrKeyStart=($nCurrentPage-1)*$pageSize;
		
		
	}
	/*
	@parm3 当前页
	作用：得到首页和前一页的链接
	*/
	public static function firstAndPrevious($url,$requestParas,$nCurrentPage){
	    $firstUp='<a href="'.$url.'?page=1'.$requestParas.'" blank="_self" >F</a>'."\n";
	    if($nCurrentPage>1){
	        $firstUp.='<a href="'.$url.'?page='.($nCurrentPage-1).$requestParas.
	       '" blank="_self" >PRE'. '</a>'."\n";
	    }
		return  $firstUp;	
	}
	/*
	@parm3 当前页
	@parm4 $nCurrentPageMax 最多可以分为多少页 ceil( 总条数除以每页的记录数 )
	作用：得到下一页和最后一页的链接
	*/
	public static function nextAndUltimate($url,$requestParas,$nCurrentPage,$nCurrentPageMax){
		$ret='';
	if($nCurrentPage<$nCurrentPageMax){
	    $ret.= '<a href="'.$url.'?page='.($nCurrentPage+1).$requestParas.
	    '" blank="_self" >NEXT'.'</a>'."\n";
	}
	    $ret=$ret.'<a href="'.$url.'?page='.($nCurrentPageMax).$requestParas.
	'" blank="_self" >END'.($nCurrentPageMax).'</a>';	
	return $ret;	
	}
	/*
	@parm1 当前网页地址
	@parm2 get 参数
	作用:自动跳转功能
	*/
    public static function autogo($url,$requestParas){
	return '<kbd><input type="text" name="custompage" size="3" style="height:14px; font-size:14px;" onkeydown="if(event.keyCode==13) {window.location=\''
 . $url .'?'.$requestParas.'&page=\'+this.value; return false;}" /></kbd>';	
	}	
	
	/*
	@parm3 $numberOfLinks 显示多少个链接
	@parm4 $countOutLinksNum 计算实际输出了多少个链接，不够的话就补上
    @parm5 $nCurrentPageMax 最多可以分为多少页 ceil( 总条除以以每页的记录数 )
	@parm6 $endLinkVal 前面输出的最一个链接的page值
	作用:补右边空缺的链接
	*/
	public static function insertLinksAtEnd($url,$requestParas,$numberOfLinks,$countOutLinksNum,$nCurrentPageMax,$endLinkVal){
		$ret = '';
		 
	    for($k=1;$k<= $numberOfLinks- $countOutLinksNum ;$k++){	
		    //这里进行最大判断超出最大页数将不会输出
	        if($endLinkVal+$k<$nCurrentPageMax){
	            $ret.='<a href="'.$url.'?page='.($endLinkVal+$k)
	            .$requestParas.'" blank="_self" style="color:green;" >'
	            .($endLinkVal+$k)."</a>\n";	
		    }
	    }	
		return $ret;
	}
	/*
	补左边空缺的链接，变量是左边空缺链接的具体内容
	$startLinkVal 前面输出的第一个链接的page值
	*/
	public static function insertLinksAtBeg(
	$url,$requestParas,$numberOfLinks,$countOutLinksNum,$startLinkVal) {	
	    $arrayLeftLinks=array();
	    for($k=1;$k<= $numberOfLinks- $countOutLinksNum ;$k++){		
            if(($startLinkVal-$k)>0){
	            $arrayLeftLinks[]='<a href="'.$url.'?page='.($startLinkVal-$k)
	            .$requestParas.'" blank="_self" style="color:red;" >'
	            .($startLinkVal-$k)."</a>\n";	
		    }
	
	    }
	     return implode(  "" , array_reverse($arrayLeftLinks) );
	}
	
	
	public static function fillContent($array,$arrKeyStart,$pageSize,&$content){
		$ic=0;
	    foreach($array as $k => $v){
		    if($arrKeyStart<=$ic and $ic<$arrKeyStart+$pageSize and $ic<count($array)){
	        $content[$k]=$v."<br>";
		    }
		    $ic++;		
	    }
	$content['sql']=' limit '.$arrKeyStart.', '.$pageSize;		
	}
	/*
	输出链接
	此时没有修正
	*/
	public static function outlinks($url,$requestParas,$numberOfLinks,$nL,$nCurrentPage,$nCurrentPageMax
	,&$countOutLinksNum,&$endLinkVal,&$startLinkVal){
		$ret = '';
	    for ($i=1;$i<$numberOfLinks+1;$i++){	
	        if(($nL+$i)==$nCurrentPage){
				$style="style='background-color:#0066cc;color:#fff'";
			}else {
				$style="";
			}
	    //$nL可能为负数
	        if(($nL+$i)>0 and ($nL+$i)<=$nCurrentPageMax){
		    $ret=$ret.'<a '.$style.' href="'.$url.'?page='.($nL+$i).$requestParas.'" blank="_self" >'
	        .($nL+$i).'</a>'."\n" ;
		     $startLinkVal=="empty"?$startLinkVal=$nL+$i : true ;
		     $endLinkVal=$nL+$i;
		     $countOutLinksNum++;
		    }
        }	
		return $ret;
	}
	/*
	设置链接样式
	*/
	public static function  style($color='#0066cc',$clstr='.pagehref'){
	$str="<style>".chr(10);
	$str.=str_repeat('  ',2).$clstr.' a{'.chr(10);	
	$str.=str_repeat('  ',2).'font-weight:bold; '.$color.' ;'.chr(10);
	$str.=str_repeat('  ',2).'border:solid 1px; '.$color.' ;'.chr(10);
	$str.=str_repeat('  ',2).'font-size:12px;'.chr(10);
	$str.=str_repeat('  ',2).'color:'.$color.';'.chr(10);
	$str.=str_repeat('  ',2).'text-decoration:none;'.chr(10);
	$str.=str_repeat('  ',2).'background-color:#fff;'.chr(10);
	$str.=str_repeat('  ',2).'padding:2px 6px 2px 6px;'.chr(10);
	$str.=str_repeat('  ',2).'margin:2px 2px  2px 2px;'.chr(10);
	$str.=str_repeat('  ',2).'overflow:hidden; line-height:100%;width:40px'.chr(10);
	$str.=str_repeat('  ',2).'}'.chr(13);
	$str.=str_repeat('  ',2).$clstr. ' a:hover{'.chr(10);
	$str.=str_repeat('  ',2).'background-color:'.$color.';'.chr(10);
	$str.=str_repeat('  ',2).'font-weight:bold;'.chr(10);
	$str.=str_repeat('  ',2).'color:#FFFFFF;'.chr(10);
	$str.=str_repeat('  ',2).'}'.chr(10);
	$str.=str_repeat('  ',2).$clstr. '{'.chr(10);
	$str.=str_repeat('  ',2).'font-size:12px;'.chr(10);
	$str.=str_repeat('  ',2).'color:'.$color.';'.chr(10);
	$str.=str_repeat('  ',2).'margin:2px 0px 2px 0px;'.chr(10);
	$str.=str_repeat('  ',2).'}'.chr(10);
	$str.=str_repeat('  ',2).$clstr.' input {'.chr(10);
	$str.=str_repeat('  ',2).'height:14px; '.chr(10);
	$str.=str_repeat('  ',2).'border:solid 1px '.$color.';'.chr(10);
	$str.=str_repeat('  ',2).'}'.chr(10);
	$str.='</style>'.chr(10);
	return $str;
}

	
}
/********************************************/
if(!function_exists('_completeFilePath2url')){
function _completeFilePath2url($completeFilePath){
    $completeFilePath=preg_replace(  array("/\\\\+/","/\/+/"),array("/","/") ,$completeFilePath);
    $_DOCUMENT_ROOT=preg_replace(  array("/\\\\+/","/\/+/"),array("/","/"),$_SERVER['DOCUMENT_ROOT']).'/';
    return str_replace($_DOCUMENT_ROOT,_get_site_url(),$completeFilePath);
}}
/********************************************/
function _escapeChars( $s ){
    $s = preg_replace( '/~/'  , '~126;' ,$s );
    $s = preg_replace( '/\\\\/' , '~92;'  ,$s );
    $s = preg_replace( '/\//' , '~47;'  ,$s );
    $s = preg_replace( '/\:/' , '~58;'  ,$s );
    $s = preg_replace( '/\*/' , '~42;'  ,$s );
    $s = preg_replace( '/\?/' , '~63;'  ,$s );
    $s = preg_replace( '/\"/' , '~34;'  ,$s ); //'39   #35  htmlspecialchars
    $s = preg_replace( '/</'  , '~60;'  ,$s );  //htmlspecialchars
    $s = preg_replace( '/>/'  , '~62;'  ,$s );  //htmlspecialchars
    $s = preg_replace( '/\|/' , '~124;' ,$s );
	
    $s = preg_replace( '/\'/' , '~39;'  ,$s ); //htmlspecialchars  
    $s = preg_replace( '/&/'  , '~38;'  ,$s );  //htmlspecialchars
			   
    $s = preg_replace( '/\{/' , '~123;' ,$s );
    $s = preg_replace( '/\}/' , '~125;' ,$s );
	
	$s = preg_replace( '/\s/' , '~s;' ,$s );
	$s = preg_replace( '/\t/' , '~t;' ,$s );
	$s = preg_replace( '/\r/' , '~r;' ,$s );
	$s = preg_replace( '/\n/' , '~n;' ,$s );
	$s = preg_replace( '/#/' , '~jing;' ,$s );
	//$s=urlencode($s);
    return $s;
}
//$str = 'http://www.jb51.net\article/23144.htm';
//echo _escapeChars( $str );
/********************************************/
function _escapeChars_2( $s ){
    $s = preg_replace( '/~/'  , '~126;' ,$s );
    $s = preg_replace( '/\\\\/' , '~92;'  ,$s );
    $s = preg_replace( '/\//' , '~47;'  ,$s );
    $s = preg_replace( '/\:/' , '~58;'  ,$s );
    $s = preg_replace( '/\*/' , '~42;'  ,$s );
    $s = preg_replace( '/\?/' , '~63;'  ,$s );
    $s = preg_replace( '/\"/' , '~34;'  ,$s ); //'39   #35  htmlspecialchars
    //$s = preg_replace( '/</'  , '~60;'  ,$s );  //htmlspecialchars
   // $s = preg_replace( '/>/'  , '~62;'  ,$s );  //htmlspecialchars
    $s = preg_replace( '/\|/' , '~124;' ,$s );
	
    $s = preg_replace( '/\'/' , '~39;'  ,$s ); //htmlspecialchars  
    //$s = preg_replace( '/&/'  , '~38;'  ,$s );  //htmlspecialchars
			   
    //$s = preg_replace( '/\{/' , '~123;' ,$s );
    //$s = preg_replace( '/\}/' , '~125;' ,$s );
	
	//$s = preg_replace( '/\s/' , '~s;' ,$s );
	// $s = preg_replace( '/\t/' , '~t;' ,$s );
	 $s = preg_replace( '/\r/' , '~r;' ,$s );
	 $s = preg_replace( '/\n/' , '~n;' ,$s );
	//$s=urlencode($s);
    return $s;
}
//$str = 'http://www.jb51.net\article/23144.htm';
//echo _escapeChars( $str );
/********************************************/
/*if(!function_exists('_file_write_contentsAPB')){
function _file_write_contentsAPB($filename = 'file.txt',$word = "你好！n"){
// 确定文件存在并且可写
	if (is_writable($filename)) {
		//打开文件
		if (!$fh = fopen($filename, 'a+b')) {
			 //echo "不能打开文件 $filename";
			 trigger_error ( "Cannot open the file '{$filename}'!" ,  E_USER_ERROR );
			 exit;
		}
		// 写入内容
		if (fwrite($fh, $word) === FALSE) {
			echo "不能写入到文件 $filename";
			exit;
		}
		echo "成功地将 $word 写入到文件 $filename";
		fclose($fh);
	} else {
		//echo "文件 $filename 不可写";
		trigger_error ( "The file '{$filename}' is not writable!" ,  E_USER_ERROR );
	}
}
}*/



if(!function_exists('_file_write_contentsAPB')){
function _file_write_contentsAPB($filename = 'file.txt',$conntent = "你好！n"){
    $file=fopen($filename,"a+b");
	$ret =fwrite($file,$conntent);
	fclose($file);
	return  $ret;
}}
/********************************************/
//php函数 读文件;
//file_readfile->_frdf
//str||str _frdf(str $filename)
//eg: _frdf('a.html')

function _frdf($filename)
{
	$file = @fopen($filename,"r");
	$ret=false;
	if($file)
	{
		while(! feof($file)){
		$ret=$ret.fgets($file);
		}
		fclose($file);
	}
    return $ret;
}
/********************************************/
//覆盖
//df_writefile_cover

if(!function_exists('_fwf')){ 
function _fwf($filename,$conntent)
{  
	$file=fopen($filename,"w+b");
	$ret =fwrite($file,$conntent);
	fclose($file);
	return  $ret;
}}
//-2EBD//
//-1EBD//
//EBD//
/********************************************/
if(!function_exists('_get_site_url')){ 
function _get_site_url(){
    return "http://".$_SERVER["SERVER_NAME"].":"
	       .$_SERVER["SERVER_PORT"]."/";    
}}
/********************************************/
//s
function _print_r($v,$left=-1,$ret="MY_tree\n"){
$left+=1;
if (is_array($v) ){
    $ret=$ret."Array\n".str_repeat('  ',$left*4)."(\n";
    foreach ($v as $key=>$val)
    {
        $ret=$ret.str_repeat('  ',$left*4+2)."[$key] => ";
        if (is_array($val)){$ret=_print_r($val,$left,$ret);}
        else {$ret=$ret."$val\n";}
        
    }
    $ret=$ret.str_repeat('  ',$left*4).")\n";
}
else 
{  $ret=$ret.$v; }
   return $ret;
}
/********************************************/
	
	
	
	
	
function _unescapeChars ($s){
	
	//$s=urldecode($s);
	 $s = preg_replace(   '/~jing;/','#' ,$s );
 $s = preg_replace(   '/~n;/',chr(10) ,$s );
 $s = preg_replace(   '/~r;/',chr(13) ,$s );
 $s = preg_replace(   '/~t;/',chr(9) ,$s );
 $s = preg_replace(   '/~s;/',chr(32) ,$s );
 
    $s = preg_replace( '/~125;/' ,'}', $s) ;			   
    $s = preg_replace( '/~123;/','{' , $s) ;
			   
    $s = preg_replace( '/~38;/','&' , $s) ;   
    $s = preg_replace( '/~39;/',"'" , $s) ; 
	
    $s = preg_replace( '/~124;/', '|' , $s) ;
    $s = preg_replace( '/~62;/' , '>' , $s) ;  //htmlspecialchars
    $s = preg_replace( '/~60;/' , '< ', $s) ;  //htmlspecialchars
    $s = preg_replace( '/~34;/' , '"' , $s) ;//'39   #35  htmlspecialchars
    $s = preg_replace( '/~63;/' , '?' ,$s) ;
    $s = preg_replace( '/~42;/' , '*' , $s) ;
    $s = preg_replace( '/~58;/' , ':' , $s) ;
    $s = preg_replace( '/~47;/' , '/' , $s) ;
    $s = preg_replace( '/~92;/' , '\\', $s) ;
    $s = preg_replace( '/~126;/', '~' , $s) ; 
    return $s;
}
//$str='http~58;~47;~47;www.jb51.net~47;article~47;23144.htm';

//echo _unescapeChars($str);
/********************************************/
//$outcom=Cmkdirs::advancedFwf('/a/root/abc/img/a.txt','aaa' );
// $outcom=Cmkdirs::advancedFwf( '/a/root/res.lrc.txt','aaa' ,__FILE__,"D:/" );
// $outcom=Cmkdirs::advancedFwf( '/a/root/res.lrc.txt','aaa' ,__FILE__ );
 //echo $outcom ? 'success' : 'failed';

class Cmkdirs{
    public function __construct(){}
	
	public function __destruct() {}
	
	//把一个或多个  /\ 替换成一个 /
	public static function  replace_backslash($s){
        return preg_replace('/\/{1,}/', '/', str_replace ('\\','/',  $s.'/'));	 
    }
	
	//通过数组一步一步的创建多个目录
	public static function  mkDirsByArray($matches,$__file__=__FILE__, $target = false ){
	    if($target==false){
	     $target = self::replace_backslash (dirname( $__file__).'/');	
	    }
	    $stauts= $target;
	    foreach($matches[1] as $v){
		$stauts=self::replace_backslash( $stauts.($v."/") );
            if(!file_exists($stauts) or !is_dir($stauts)){
			    echo($stauts."<br>");
			    $r=@mkdir($stauts,0777);
			    echo $r?"ok<br>":"error<br>";
	        }else{
				echo $stauts.': 已经存在<br>';
			}	
	    }
	return $stauts;
    }		
	
	/*创建目录高级
	@parm1 $comdir 完整的目录名
	@parm3 $target 目标盘符 
	*/
	public static function mkdir ($comdir,$__file__,$target= false ){
	    preg_match_all("/\/{1,}([^\/]{1,})/",$comdir,$ms);
	    return self::mkDirsByArray($ms,$__file__,$target);
    }
	
	/*写文件
	@parm1 $filename 文件名
	@parm2 $conntent 文件内容
	*/
	public static function fwf($filename,$conntent){  
	    $file=fopen($filename,"w+b");
	    $ret =fwrite($file,$conntent);
	    fclose($file);
	    return  $ret;
    }
	
	/*写文件高级
	@parm1 $filename 文件名
	@parm2 $conntent 文件内容
	@parm3 $target   目标盘符
	*/
	public static function advancedFwf($filename, $conntent ,$__file__=__FILE__,$target= false){  
         $dirpath = self::mkdir( dirname($filename) ,$__file__ ,$target );
		 
		 $filename = preg_replace('/.*?([^\/]*)$/','\\1',$filename);
		 return self::fwf($dirpath.$filename,$conntent);
    }

}
/********************************************/
class Cscandir{
    public $path;
	public $completeFiles ;
	public $completeDirs ;
	public $files;
	public $dirs ;
	public $filesDirs ;
	public $completeFilesDirs ;
	
	//把文件转化为文件所在目录字串
	//eg: I:/AppServ/www/_App_/inc/baseLib.php
    //变为 I:/AppServ/www/_App_/inc/
	public static function fileToDirname($path){
	    if( is_file($path) ){
		    return dirname($path);	
		} 
		return $path;
	}
    public function  __construct($path="I:/_phplib/",$sortingOrder=0){
		$path = Cscandir::fileToDirname($path);
		$this->path = $path ;
		$this->scandir($path , $sortingOrder);
		$this->getCompleteFilesDirs();
		$this->getCompleteDirs();
		return $this->filesDirs;
	}
	//  第一步
	public function  scandir($path='',$sortingOrder=0){
		if(empty($path)){
		    $path=$this->path;	
		}
		//获取目录下的所有文件s和文件夹s  scandir可用dir替代
 		$this->filesDirs=scandir ( $path , $sortingOrder ) ;  
		return $this->filesDirs;       
		//sort()升序  rsort()降序 ;
	}
	
	//得到完整路径的目录和文件
	public function getCompleteFilesDirs(){
		if(count($this->completeFilesDirs)==0){
			foreach($this->filesDirs as $k => $v ){
				$this->completeFilesDirs[] = self::formatPath($this->path."/".$v);   	
			}
		}
		return $this->completeFilesDirs;
		
	} 
	public function getFiles($regexp=false){
		$fils=array();
	        foreach($this->filesDirs as $k => $v ){
				if(!is_dir($this->path."/".$v)  ){
					 
					$fils[] =$v ; 

				}
			}	
		return $fils;
	}

	public function getDirs(){
		$dirs=array();
	        foreach($this->filesDirs as $k => $v ){
				if(is_dir($this->path."/".$v))
				$dirs[] =$v ;   	
			}	
		return $dirs;
	}
	
	//得到完整的目录文件名
	public function getCompleteDirs(){

		if(count($this->completeDirs)==0){
			foreach($this->completeFilesDirs as $k=>$v){			
				if(is_dir($v)){
					$this->completeDirs[]=$v;
				}else{
					$this->completeFiles[]=$v;	
				}	
			}
		}
		return $this->completeDirs;
		
	}
	public function getCompleteFiles(){
		return $this->completeFiles;
	}	
	public static function formatPath($str){		
	    return preg_replace (     '/\/{1,}/' , '/' ,
	           str_replace ( '\\' , '/' , $str )   ) ;	
    }	 
}
/********************************************/
class Cdb {
	public $querynum = 0;
	public $link;
	public $histories;
	public $dbhost;
	public $dbuser;
	public $dbpw;
	public $dbname = '' ; //
	public $dbcharset = 'utf8'; //
	public $pconnect = 0; //
	public $tablepre = '' ;
	public $goneaway = 5;
	function __construct(){
		
	} 
	function connect($dbhost='localhost', $dbuser='root', $dbpw='123654' ) {
		$this->dbhost = $dbhost;
		$this->dbuser = $dbuser;
		$this->dbpw = $dbpw;
	    if($this->pconnect) {
			if(!$this->link = mysql_pconnect($dbhost, $dbuser, $dbpw)) {
				$this->halt('Can not connect to MySQL server');
			}
		} else {
			if(!$this->link = mysql_connect($dbhost, $dbuser, $dbpw)) {
				$this->halt('Can not connect to MySQL server');
			}
		}

		if($this->version() > '4.1') {
	        $this->setCharacterCollation();	 
			if($this->version() > '5.0.1') {
				mysql_query("SET sql_mode=''", $this->link);
			}
		}

		if($this->dbname) {
			mysql_select_db($dbname, $this->link);
		}

	}
	function setCharacterCollation(){
	    mysql_query( "SET character_set_client     = utf8" , $this->link);
        mysql_query( "SET character_set_connection = utf8" , $this->link);
        mysql_query( "SET character_set_database   = utf8" , $this->link);
        mysql_query( "SET character_set_results    = utf8" , $this->link);
        mysql_query( "SET character_set_server     = utf8" , $this->link);
        mysql_query( "SET collation_connection     = utf8" , $this->link);
        mysql_query( "SET collation_database       = utf8" , $this->link);
        mysql_query( "SET collation_server         = utf8" , $this->link);
	}
    function select_db($dbname){
	    return mysql_select_db($dbname, $this->link);
	}
	function fetch_array($query, $result_type = MYSQL_ASSOC) {
		return mysql_fetch_array($query, $result_type);
	}

	function result_first($sql) {
		$query = $this->query($sql);
		return $this->result($query, 0);
	}

	function fetch_first($sql) {
		$query = $this->query($sql);
		return $this->fetch_array($query);
	}

 
	function fetch_all($sql) {
	    $arr=array();
		$query = $this->query($sql);
		if(""<>$query){
			while($data = $this->fetch_array($query)) {
				$arr[] = $data;
			}
		}
		return $arr;
	}

	function cache_gc() {
		$this->query("DELETE FROM {$this->tablepre}sqlcaches WHERE expiry<$this->time");
	}

	function query($sql, $type = '', $cachetime = FALSE) {
		$func = $type == 'UNBUFFERED' && @function_exists('mysql_unbuffered_query') ? 'mysql_unbuffered_query' : 'mysql_query';
		if(!($query = $func($sql, $this->link)) && $type != 'SILENT') {
			$this->halt('MySQL Query Error', $sql);
		}
		$this->querynum++;
		$this->histories[] = $sql;
		return $query;
	}

	function affected_rows() {
		return mysql_affected_rows($this->link);
	}

	function error() {
		return (($this->link) ? mysql_error($this->link) : mysql_error());
	}

	function errno() {
		return intval(($this->link) ? mysql_errno($this->link) : mysql_errno());
	}

	function result($query, $row) {
		$query = @mysql_result($query, $row);
		return $query;
	}

	function num_rows($query) {
		$query = mysql_num_rows($query);
		return $query;
	}

	function num_fields($query) {
		return mysql_num_fields($query);
	}

	function free_result($query) {
		return mysql_free_result($query);
	}

	function insert_id() {
		return ($id = mysql_insert_id($this->link)) >= 0 ? $id : $this->result($this->query("SELECT last_insert_id()"), 0);
	}

	function fetch_row($query) {
		$query = mysql_fetch_row($query);
		return $query;
	}

	function fetch_fields($query) {
		return mysql_fetch_field($query);
	}

	function version() {
		return mysql_get_server_info($this->link);
	}

	function close() {
		return mysql_close($this->link);
	}

	function halt($message = '', $sql = '') {
		$error = mysql_error();
		$errorno = mysql_errno();
		if($errorno == 2006 && $this->goneaway-- > 0) {
			$this->connect($this->dbhost, $this->dbuser, $this->dbpw, $this->dbname, $this->dbcharset, $this->pconnect, $this->tablepre, $this->time);
			$this->query($sql);
		} else {
			$s = '<b>Error:</b>'.$error.'<br />';
			$s .= '<b>Errno:</b>'.$errorno.'<br />';
			$s .= '<b>SQL:</b>:'.$sql;
			exit($s);
		}
	}
}

 
/********************************************/
/*fileName: 
 *internal version: 0001
 *version : 1.0.0 [programming] [official version]
 *author : HeShengyu
 *Date : 2014.08.27 
 *email: koolfer@163.com
 *end date:2014.08.27
 *http://www.cnblogs.com/ssnowy/
*/
class Cldir{
	public static function writeFileW1b($filename,$conntent){ 
	    $file=fopen($filename,"w+b");
		if(is_array($conntent)){
            foreach($conntent as $v){
				//将数组写入文件，每次追加一个数组的内容　
			    $ret =fwrite($file,$v);	
			}	
		}else{
		     $ret =fwrite($file,$conntent);
		}
	    fclose($file);
	    return  $ret;
    }	
	//Cldir::writeFile(
    //array('/a1koolfer/b/d/e/f/g/h/o',__FILE__)
    //,'abc.txt','文件内容'.date('Y-m-d H:i:s',time())
    //);
	public static function writeFile($path,$filename,$conntent='conntent'){
		if(count($path)==1){
	        $thePath=self::mkdir($path[0]);
		}
		if(count($path)==2){
			$thePath=self::mkdir($path[0],$path[1]);
		}
		//echo '{'.$thePath.'}';	
		$result=self::writeFileW1b($thePath.$filename,$conntent);
		echo '生成文件:'.$thePath.$filename.'</br>'; 
		echo $result?'ok</br>':'error</br>';
	}
	public static function mkdir($path,$__file__=''){
	    if(!empty($__file__)){
		    return self::mkdirNotTarget($path,$__file__);	
		}
		return self::mkdirs($path);	
	}
	public static function mkdirs($path){
    $target=preg_replace('/([A-Z]:[\/\\\\]){0,1}[\d\D]{0,}/','\\1',$path);
	if($target==''){
	    trigger_error("Cldir::mkdir,目标磁盘未定义",E_USER_ERROR);	
	}
    preg_match_all("/\/{1,}([^\/]{1,})/",$path,$matches);
	$stauts= $target;
		foreach($matches[1] as $v){
			$stauts=$stauts.($v."/");
			if(!file_exists($stauts) or !is_dir($stauts)){
				echo($stauts."<br>");
				echo mkdir($stauts,0777)
				?"<font color='#FF6600''>create dir ok</font><br>"
				:"<font color='#FF6600''>create dir error</font><br>";
			}else{
				echo $stauts.'目录已经存在</br>';	
			}
		}
	return $stauts;
    }	
	public static function mkdirNotTarget($path,$__file__){
		if(empty($path) or empty($__file__)){
		    trigger_error("Cldir::mkdirNotTarget,参数1,2都不能为空,
			参数1例如:/a/b参数2可以为__FILE__",E_USER_ERROR);	
			exit;	
		}
	     preg_match_all("/\/{1,}([^\/]{1,})/",$path,$matches);	
		 $stauts= self::getCurrentDir($__file__);
			foreach($matches[1] as $v){
				$stauts=$stauts.($v."/");
				if(!file_exists($stauts) or !is_dir($stauts)){
					echo($stauts."<br>");
					echo mkdir($stauts,0777)
					?"<font color='#FF6600''>create dir ok</font><br>"
					:"<font color='#FF6600''>create dir error</font><br>";
				}else{
					echo $stauts.'目录已经存在</br>';	
				}
			}
		return $stauts;	
	}
	public static function getCurrentDir($__file__){
		return preg_replace( array('/\/{1,}/'), array('/'),
	    str_replace( '\\','/' , dirname($__file__).'/' )   
		       );
		 	
	}
}
/********************************************/
/*//禁用错误报告
error_reporting(0);

//报告运行时错误
error_reporting(E_ERROR | E_WARNING | E_PARSE);

//报告所有错误
error_reporting(E_ALL);*/
 
class clerror{
    public static $cltab;
	public function __construct(){
	    self::$cltab=new Cltable();	
		$this->setErrorHandler();
	}
	public function __destruct(){}	
	public function setErrorHandler(){
	    set_error_handler ('clerror::errorHandler');
    }
	
	//实际上是系统遇到错误时的回调函数。语法结构有  window.addEventListener('resize',Callback,flase)相似
	//clerror::errorHandler 相当于callback
	public static function errorHandler (
	$error_level, $error_message, $file, $line,$error_context) {
 		 if (!(error_reporting() & $error_level)) {
        // This error code is not included in error_reporting
        return;
        } 
		//global $tab;
    switch ($error_level) {
		//提醒级别
        case E_NOTICE:
		 $error_type = '<span style="color:#099;">E_NOTICE</span>';
            break;
        case E_USER_NOTICE:
            $error_type = '<span style="color:#093;">E_USER_NOTICE</span>';
            break;
		
		//警告级别
        case E_WARNING:
		$error_type = '<span style="color:#660;">E_WARNING</span>';
            break;
        case E_USER_WARNING:
            $error_type = '<span style="color:#660;">E_USER_WARNING</span>';
            break;
		
		//错误级别
        case E_ERROR:
		    $error_type = '<span style="color:red;">E_ERROR</span>';
            break;
        case E_USER_ERROR:
            $error_type = '<span style="color:#f60;">E_USER_ERROR</span>';
            $EXIT = TRUE;
            break;
		
		//其他未知错误
        default:
            $error_type = '['.$error_level.']';
            $EXIT = TRUE;
            break;
    }
 
	if(array_key_exists("php_errormsg", $error_context)){
		$php_errormsg=$error_context['php_errormsg'];
	}else{
	    $php_errormsg='';	
	}
	
	$desc='<b><span style="color:#f80;">'.$error_type.':</span></b> <span style="color:#3a3;">'.$error_message
	.'</span><span style="color:#80f;"> in </span><span style="color:#f08;"><b>'
	.$file.'</b></span><span style="color:#80f;"> on line </span><b><span style="color:#880;">'
	.$line.'</b>';
	
	self::$cltab->export (array(
	'level'=>$error_level,
	'type'=>$error_type,
	'message'=>$error_message,
	'file'=>$file, 
	'line'=>$line,
	'error_context'=> $php_errormsg
	,'-description-'=>$desc 
	 ));
     //return true 隐藏系统的报错;
        return true;
     }
}
/********************************************/
class clfile{
	//覆盖
    //writefile_cover
	public static function writeFileW1b($filename,$conntent){ 
	    $file=fopen($filename,"w+b");
	    $ret =fwrite($file,$conntent);
	    fclose($file);
	    return  $ret;
    }
	
	public static function loadFiles($aFileNames,$match='',&$arr=''){
	$aRet=array();
	$arr =array();
    foreach($aFileNames as $v){
		if($match=='' || preg_match($match,$v)){
			$arr[]=$v;
		$aRet[]=
							/*implode( "",file($v) 为一字符串　file($v为数组，　*/ 
							/*implode() 函数把数组元素组合为一个字符串。
							 file($v)把指定文件读到数组中
							 array_slice取数组的第1到倒数第一 ，第0和最后一个不取
							 array_slice(array,offset,length,preserve)	*/
						  
						  implode( "",  array_slice( file($v) , 1 , -1 )  ) 
						   ."/********************************************/\n"   ;
		}
	}
	     
	return $aRet;
	
    }	
	
	public static function getFileNames($aFileNames,$match='',&$arr=''){
 
	$arr =array();
    foreach($aFileNames as $v){
		if($match=='' || preg_match($match,$v)){
			$arr[]=$v;
		}
	}
	     
	return $arr;
	
    }	
	
}

/********************************************/
class Clscandir{
    public $path;
	public $completeFiles ;
	public $completeDirs ;
	public $files;
	public $dirs ;
	public $filesDirs ;
	public $completeFilesDirs ;
	
	//把文件转化为文件所在目录字串
	//eg: I:/AppServ/www/_App_/inc/baseLib.php
    //变为 I:/AppServ/www/_App_/inc/
	public static function fileToDirname($path){
	    if( is_file($path) ){
		    return dirname($path);	
		} 
		return $path;
	}
    public function  __construct($path="I:/_phplib/",$sortingOrder=0){
		$path = Clscandir::fileToDirname($path);
		$this->path = $path ;
		$this->scandir($path , $sortingOrder);
		$this->getCompleteFilesDirs();
		$this->getCompleteDirs();
		return $this->filesDirs;
	}
	//  第一步
	public function  scandir($path='',$sortingOrder=0){
		if(empty($path)){
		    $path=$this->path;	
		}
		//获取目录下的所有文件s和文件夹s  scandir可用dir替代
 		$this->filesDirs=scandir ( $path , $sortingOrder ) ;  
		return $this->filesDirs;       
		//sort()升序  rsort()降序 ;
	}
	
	//得到完整路径的目录和文件
	public function getCompleteFilesDirs(){
		if(count($this->completeFilesDirs)==0){
			foreach($this->filesDirs as $k => $v ){
				$this->completeFilesDirs[] = self::formatPath($this->path."/".$v);   	
			}
		}
		return $this->completeFilesDirs;
		
	} 
	public function getFiles($regexp=false){
		$fils=array();
	        foreach($this->filesDirs as $k => $v ){
				if(!is_dir($this->path."/".$v)  ){
					 
					$fils[] =$v ; 

				}
			}	
		return $fils;
	}
	//排除 .html文件
	//getFiles_nomatch( "/(\.html)$/");
	public function getFiles_nomatch($regexp=false){
		$fils=array();
	        foreach($this->filesDirs as $k => $v ){
				if(!is_dir($this->path."/".$v)  ){
					if(!$regexp){
					$fils[] =$v ; 
					}else{
						if(!preg_match($regexp,$v)){
							$fils[] =$v ; 
						}	
					}
				}
			}	
		return $fils;
	}
	public function getFiles_match($regexp=false){
		$fils=array();
	        foreach($this->filesDirs as $k => $v ){
				if(!is_dir($this->path."/".$v)  ){
					if(!$regexp){
					$fils[] =$v ; 
					}else{
						if(preg_match($regexp,$v)){
							$fils[] =$v ; 
						}	
					}
				}
			}	
		return $fils;
	}
	public function getDirs(){
		$dirs=array();
	        foreach($this->filesDirs as $k => $v ){
				if(is_dir($this->path."/".$v))
				$dirs[] =$v ;   	
			}	
		return $dirs;
	}
	
	//得到完整的目录文件名
	public function getCompleteDirs(){

		if(count($this->completeDirs)==0){
			foreach($this->completeFilesDirs as $k=>$v){			
				if(is_dir($v)){
					$this->completeDirs[]=$v;
				}else{
					$this->completeFiles[]=$v;	
				}	
			}
		}
		return $this->completeDirs;
		
	}
	public function getCompleteFiles(){
		return $this->completeFiles;
	}	
    
	//把文件数组变为网页的href地址
	public static function toUrl($arr,$target='_blank'){
	    $ret='';
		foreach($arr as $v){
		    $ret.= '<li><a href="'.$v.'?r='.rand(1,9999999).'" title="'.$v.'" target="'.$target.'">'
			.preg_replace('/(.{0,}?)([0-9a-zA-Z\-_\.\$]{0,})(html|php|htm|txt)$/','\\1\\2\\3',$v)
			.'</a></li>';	
 	
		}	
		return $ret;
	}

	public static function formatPath($str){		
	    return preg_replace (     '/\/{1,}/' , '/' ,
	           str_replace ( '\\' , '/' , $str )   ) ;	
    }
	

	public static function toHttpUrl($arr,
	$middle_path="/_doll/_ae/"){
		$severpath="http://".$_SERVER['SERVER_NAME']
		.":".$_SERVER['SERVER_PORT']
		.$middle_path;
        $retarr=array();
		foreach($arr as $k=>$v){
			 $v=preg_replace(
	  "/[\d\D]*\/([a-zA-Z0-9_\.\-\[\]\(\)]*\.(?:flv|f4v|mp[4]|mkv))$/"
       ,$severpath."\\1",$v);
	       if(!preg_match("/^[a-zA-Z]\:/",$v))
		   {
			   $retarr[$k]=Clscandir::replacePoundsign($v); 	
		   }	
		}
		return $retarr;		
	}
	public static function replacePoundsign($str){
        $str=iconv('GB2312','UTF-8', $str)  ;
	    $str= preg_replace("/#/","%23",$str);
		return $str;
	}	
	public  function getHttpUrls($wwwPath,$arr=''){
		
		if( empty($arr) ){
			$arr = $this->getCompleteFiles() ;
		}
		
		$retarr=array();
		foreach($arr as $k=>$v){
			$retarr[]=self::filetourl($wwwPath,$v);
		}
		return $retarr;
	}
	public static function filetourl($wwwPath,$filename,$s=''){
		
		$server_url="http:".$s."//".$_SERVER['SERVER_NAME'].":".$_SERVER['SERVER_PORT']."/";
		return str_replace(array($wwwPath,"#"),array($server_url,"%23"),$filename);
		
	}
}
/********************************************/
class Clstr{
    public static function sdefault($str,$defaultValue){
	    if(strlen($str)==0){
		    return $defaultValue; 
		}
		return $str;	
	}	
}
/********************************************/

class sys{
	public static function _print_r($v,$left=-1,$ret="My"){
		$left+=1;
		if (is_array($v) ){
			return self::arrayToString($v,$left,$ret);
		} 
		return   $ret.$v."\n";	 
	}

	public static function arrayToString($v,$left,$ret){	
			$ret.="Array\n".str_repeat('  ',$left*4)."(\n";
			foreach ($v as $key=>$val)
			{
				$parmret=$ret.str_repeat('  ',$left*4+2)."[$key] => ";
				$ret=self::recursionPrintR($val,$left,$parmret)	;			 
			}
			 $ret.=str_repeat('  ',$left*4).")\n\n";	
		 return $ret;
	}
 
//递归调用_print_r
	public static function recursionPrintR($val,$left,$ret){	
		if (is_array($val)){
			return self::_print_r($val,$left,$ret);
		}
			return $ret."$val\n";	 		 	
	}
	public static function print_r($v,$left=-1,$ret="My"){
	    echo self::_print_r($v,$left,$ret);
	}
}
/********************************************/
class Cltable{	
    public static $emptyTable;
	public $carray;
	public $tableContent;
	public $tdWidth;
	public $isShowKey=false;
	public function setTdWidth($num='200'){
	    $this->tdWidth =$num;	
	}
	public function setPreTdWidth($num='400'){
	    $this->tdpreWidth =$num;	
	}
	const Width="65px";
    public function __construct($arr='empty',$sWidth=''){
		//设置表格开头的html 代码
		self::setEmptyTable();
		//要装入表格的数组
		$this->tableContent=array();
		//检查要装入的数组是不是为空
		$this->checkArray($arr);
		$arrcontent=$this->carray;
		$this->tdWidth='200';
		$this->tdpreWidth =400;
		//$arrcontent 实际上就是经过处理的 $arr; 也就是构造函数的参数1
		//step 1
		$this->mkContent($arrcontent,$sWidth);
	}
	public static function setBg($num){
		return  ($num%2)==0 ? "#fafafa;" : "#fefefe;";
	}
    public function export($arr='empty',$sWidth='200'){
		    // 
			//直接输出 $this->tableContent
		if($arr=='empty'){
			echo Cltable::$emptyTable;	
			foreach( $this->tableContent as $k=>$v){
				echo $v;	
			}
			 echo '</table></div><style>'
			 .' .num { color:#1184CE;font-size:18px; 
			   ;overflow:hidden;border-left:1px solid #ccc ;width:'.$this->tdWidth.'px; }.num b{color:#999;}'
			   .' .pretd{width:'.$this->tdpreWidth.'px;}</style>';
		}else{
			//不为空直接输出
			$this->exportArr($arr,$sWidth);
		}
	}
	public function exportArr($arr,$sWidth=''){
		//设置好内容后就可以用export输出了
	    $this->mkContent($arr,$sWidth);	
		$this->export();
	}
	//把数组变成表格html代码的中间部分，就是tr td格式的字串
	//step 
	public function mkContent($arr,$sWidth){	
	    $i=0;
		$this->tableContent=array();
		foreach( $arr as $k=>$v){	
			$this->tableContent[]="<tr><td  class='num' "
			 ." style='display:block;color:#999;font-size:18px;"
			 ."width:".$sWidth."px;overflow:hidden;'><b>".$k
             ."</b></td><td style='border-color:#ccc;width:4000px;"
			 ." border-left-style:solid; border-left-width:1px;"
			 ."height:12px; padding-left:2px;background-color:"
			 .self::setBg($i)."'>".$v."</td></tr>";
			 $i++;
		}
		
    }
	public function __destruct(){}	
	public function checkArray($arr){
		!is_array($arr) ? $arr=array('Is not array!') : true;
		$this->carray=$arr;
	}
	public static function setEmptyTable(){
	   Cltable::$emptyTable =
	   '<div style="border:1px #ccc solid; margin-top:12px;color:#1184CE;" onDblClick="this.style.display=\'none\';"><table  border="0" cellspacing="0" cellpadding="0" style="background-color:#fff;" class="tablearr" >';	
	}
	
}
final class CltableEx extends Cltable{  
    
    public function mkContent($arr,$sWidth){	
	    $i=0;
		$this->tableContent=array();
		foreach( $arr as $k=>$v){
		
			$this->tableContent[]=
			 $this->arrFormatTrtd($v,$i,$sWidth,$k);
			 $i++;
		}
		
    }
	public function arrFormatTrtd($arr,$i,$sWidth,$k){
		$tmpk=$k;
	    $ret="<tr style='display:block;color:#999;font-size:18px;"
			 ." ".";overflow:hidden;border-left:1px solid #ccc;background-color:"
			 .parent::setBg($i)."'>";
		
		if(is_array($arr)){
			$ii=0;
			
			$ret.="<td><div class='pretd'   >"
			 ." <b>ARR:[".$k."]</b></div></td>";
			 
		    foreach($arr as $key2=>$value){
				
				$keyv= $ii<1 ? $k : $key2;
				if($this->isShowKey){
				$ret.="<td><div>"." <b> " .$key2."</b></td></div>\r\n\r\n";
				}
				$ret.="<td><div class='num' >"._print_r($value,-1,"")."</td></div>";
			$ii++; 
				
				
			}
		}else{
			
			$ret .= "<td><div class='pretd'  "
			 . "><b>STR:[".$tmpk
             ."]</b></td><td></div><div class='num'>".$arr."</div></td>";
			  
        }
		
		$ret.="</tr>";
		return $ret;	
		
	}   
}
/********************************************/
if(!function_exists('_frdf')){
function _frdf($filename)
{
    $file = @fopen($filename,"r");
    $ret=false;
    if($file)
    {
        while(! feof($file)){
        $ret=$ret.fgets($file)."";
        }
        fclose($file);
    }
    return $ret;
}
}

if(!function_exists('_fwf')){
function _fwf($filename,$conntent)
{  
    $file=fopen($filename,"w+b");
    $ret =fwrite($file,$conntent);
    fclose($file);
    return  $ret;
}}

if(!function_exists('get_current_url')){ 
function get_current_url(){
    return "http://".$_SERVER["SERVER_NAME"].":"
	       .$_SERVER["SERVER_PORT"]."".$_SERVER["SCRIPT_NAME"];    
}}

function showinfo($bl){
	if($bl){
	    return 'ok<br>';
	}else{
	    return 'error<br>';	
	}
}
//从http地址中取得文件名
//([0-9a-zA-Z\-_\.\$]{0,})这个是文件名的配项;

if(!function_exists('url2filenmae')){
function url2filenmae($s){
	return preg_replace('/.{0,}?([0-9a-zA-Z\-_\.\$]{0,})\.(html|php|htm)$/','\\1',$s);	
}}

if(!function_exists('url2filename')){
function url2filename($s){
	return preg_replace('/.{0,}?([0-9a-zA-Z\-_\.\$]{0,})\.(html|php|htm)$/','\\1',$s);	
}}


if(!function_exists('generation_selfhtml')){
function generation_selfhtml($str=''){
	if(array_key_exists("a", $_REQUEST)){
	if($_REQUEST["a"]=="gener"){
    $httpurl=get_current_url() ;
	//$conntent=_frdf($httpurl)	;
	$conntent=file_get_contents($httpurl)	;	
	echo htmlspecialchars($conntent); 
	
	$filename=''.url2filenmae($httpurl).$str.".html";
	
	echo showinfo( _fwf($filename,$conntent) )  ;	
	}
	}
    echo '<iframe id="gener" src="'.get_current_url().'?a=gener" style="display:none;" height="40" frameborder="0"></iframe>'	;
	 
	 
}}

if(!function_exists('generation_otherhtml')){
function generation_otherhtml($str='',$httpurl,$newname=''){
	if(array_key_exists("a", $_REQUEST)){
	if($_REQUEST["a"]=="gener"){

	$conntent=file_get_contents($httpurl)	;	
	echo htmlspecialchars($conntent); 
	if(empty($newname)){
	$newname =$httpurl;	
	}
	$filename= url2filenmae($newname).$str.".html";
	
	echo showinfo( _fwf($filename,$conntent) )  ;	
	}
	}
    echo '<iframe id="gener" src="'.get_current_url()
	.'?a=gener" style="display:block;" height="800" width="100%" scrolling="no" frameborder="0"></iframe>'	;
	 
	 
}}


/********************************************/

function getRealIpAddr()
{
    if (!empty($_SERVER['HTTP_CLIENT_IP']))
    {
        $ip=$_SERVER['HTTP_CLIENT_IP'];
    }
    elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
    //to check ip is pass from proxy
    {
        $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
    }
    else
    {
        $ip=$_SERVER['REMOTE_ADDR'];
    }
    return $ip;
}
 
/********************************************/

if(!function_exists('otherhtml2url')){
function otherhtml2url($dirname,$filename){
	$dirname=str_replace( '\\','/' ,$dirname);
    //$dirname=preg_replace( array("/\/+/"),array("/") ,$dirname);
    $_DOCUMENT_ROOT=preg_replace( array("/\\\\/","/\/+/"),array("/","/"),$_SERVER['DOCUMENT_ROOT']).'/';
    return str_replace($_DOCUMENT_ROOT,_get_site_url(),$dirname."/").$filename;
 
}}
/********************************************/
//s
function pre_print_r($arr){
    echo '<pre>';
    print_r($arr);
    echo '</pre>';    
}
//e
/********************************************/
function shtmlspecialchars($string) {
	if(is_array($string)) {
		foreach($string as $key => $val) {
			 $string[$key] = shtmlspecialchars($val);		 
		}
	} else {
	$string =str_replace(array('&', '"', '<', '>'), array('&amp;', '&quot;', '&lt;', '&gt;'), $string);
	}
	return $string;
}
/********************************************/
function test(){
    echo time(); 
}
/********************************************/

?>